#!/usr/bin/env python3
import sys
import psutil

def get_iface_ip(iface_name: str) -> str:
    addrs = psutil.net_if_addrs()
    if iface_name in addrs:
        for snic in addrs[iface_name]:
            if snic.family.name == 'AF_INET':  # IPv4
                return snic.address
    return None

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("用法: python3 get_host_ip.py <网卡名>")
        print("示例: python3 get_host_ip.py eth0")
        sys.exit(1)

    iface = sys.argv[1]
    ip = get_iface_ip(iface)
    if ip:
        print(ip)
    else:
        print(f"未找到网卡 {iface} 或未分配IPv4地址")
